
define(["jquery", "magnifier", "front", "carOperate", "validateLogin"], function (a, Magnifier, Front, CarOperate, user) {
    'use strict';
    let result = "";
    let find = "";
    let data = {};
    let carOperate = "";
    function init() {
        var magnifier = new Magnifier(" .img-small-container img", ".detail-img-big img", "mouseenter", imgCb);
        magnifier.init();
        var magnifier_xiaobao = new Magnifier(" .protection-items li", ".content-left img", "mouseenter", xiaobaoCb);
        magnifier_xiaobao.init();
        var headHeight = $(".describe").offset().top;
        var front = new Front(".head-menu-container", headHeight);
        front.init();
        console.log(data)

        // $("#btn").on(click,onAddToCar);
    }
    function xiaobaoCb(currentItem) {
        $(currentItem).find("span").addClass("active");
        $(currentItem).siblings().find("span").removeClass("active");
    }
    function imgCb(currentItem) {
        $(currentItem).addClass("active");
        $(currentItem).parent().siblings().find("img").removeClass("active");
    }
    function render() {
        renderHeader();
        renderMagnifierLeft();
        renderMagnifierRight();
        renderSelDetail();
        renderProductDetail();
        // renderShopBag();
        setTimeout(() => {
            $(".submitBtn").on("click", onAddToCar);
        }, 500);
    }
    function renderShopBag(){
        carOperate = new CarOperate();
        data = carOperate.getStorage();
        console.log(data)
        $(".buyNum").html(carOperate.getTotalCount());
    }
    function renderHeader() {
        var html = "";
        html += `
        <span>> ${find.short_name}</span>`;
        $(".title").html(html);
    }
    function renderMagnifierLeft() {
        var html = "";
        html += `<li><img
                        src="${find.hd_thumb_url}"
                        alt=""></li>
                <li><img
                        src="${find.image_url}"
                        alt=""></li>
                
                 `
        $(".img-small-container").html(html);
    }
    function renderMagnifierRight() {
        var html = "";
        html += `<img src="${find.hd_thumb_url}" alt="">
                <img src="${find.image_url}" alt="">
                 `
        $(".detail-img-big").html(html);
    }
    function renderSelDetail() {
        var html = "";
        html += `<h2 class="product-name">${find.goods_name}</h2>
        <div class="priceContainer">
            <div class="price">
                <span class="price-now">${(find.normal_price) / 100}</span>
                <span class="price-refer marginLeft">${(find.market_price) / 100}</span>
                <span class="reduce">剩余:3天</span>
            </div>
            <div class="price-vice">
                <span class="lable">促销:</span>
                <i class="type marginLeft">满减件</i>
                <span class="nomal">满一件减3元 详情>></span>

            </div>
            <div class="price-deliver">
                <span class="lable">配送:</span>
                <span class="marginLeft">宁波市 至
                    <span class="address"><i class="arrow"></i></span>
                    包邮, 24小时发货
                </span>
            </div>
            <div class="price-color">
                <span class="lable float">颜色:</span>
                <div class="marginLeft float">
                    <ul class="sel-container">
                        <li>[加大款]夏凉被</li>
                        <li>[加大款]夏凉被</li>
                        <li>[加大款]夏凉被</li>
                        <li>[加大款]夏凉被</li>
                    </ul>
                </div>
            </div>
            <div class="price-code">
                <span class="lable float">尺码:</span>
                <div class="marginLeft float">
                    <ul class="sel-container">
                        <li>155</li>
                        <li>160</li>
                        <li>165</li>
                        <li>170</li>
                    </ul>
                </div>
            </div>
            <div class="price-button">
                <span class="lable float">尺码:</span>
                <div class="marginLeft float">
                    <i class="plus">-</i>
                    <span class="plus num">1</span>
                    <i class="plus">+</i>
                </div>

            </div>
            <div class="price-submit">
                <span class="buy  car">立即购买</span>
                <span class="car addCar submitBtn">加入购物车</span>
            </div>
        </div>`
        $(".sel").html(html);
    }
    function renderProductDetail() {
        var html = "";
        html += `<img src="${find.hd_thumb_url}" alt="">
                <img src="${find.image_url}" alt="">
                 `
        $(".discrible-main-detail").html(html);
    }
    function getData(goods_id) {

        var carOperate = new CarOperate();
        var data = carOperate.queryProduct();
        if (data == {}) {
            getAjax();
            return false;
        }
        result = JSON.parse(localStorage.getItem("list"));
        result.some((item) => {
            if (item.goods_id == goods_id)
                return find = item;
        })
        render();
        var timer = setTimeout(() => {
            init();
        }, 0);
    }
    function getAjax() {
        // let url = "pxx/";
        // let data = {
        //     subject_id: subject_id,
        //     page: page,
        //     size: 40,
        //     // province_id: 25,
        //     // platform: 1,
        //     // pdduid: 0
        // }

        let url = "http://zhaoyx0907.com/pxx";
        $.ajax(url,
            {
                // data: data,
                dataType: "json"
            }
        ).then(function (res) {
            result = res.goods_list;
            result.some((item) => {
                if (item.goods_id == goods_id)
                    return find = item;
            })
            render();
            var timer = setTimeout(() => {
                init();
            }, 0);
        })
    }
    function onAddToCar() {
        carOperate = new CarOperate();
        data = carOperate.addProduct(find.cat_id1, find.goods_id, 1, find.normal_price);
        renderShopBag();
    }

    $(function () {
        // alert(window.obj)
        var location = window.location.href;
        var _id = location.split("?")[1];
        var goods_id = _id ? _id.split("#")[0] : _id;
        goods_id = goods_id ? goods_id : "523096";
        getData(goods_id);
        // init();

    })
});